<template>
	<view class="container">
		<up-toast ref="uToastRef"></up-toast>

		<view class="header">
			<text class="title">手机号登录</text>
			<text class="subtitle">请输入手机号完成登录</text>
		</view>

		<view class="form">
			<!-- 手机号输入 -->
			<view class="input-box">
				<view class="country-code" @click="selectCountry">
					<text>+86</text>
					<text class="arrow">›</text>
				</view>
				<up-input v-model="phoneForm.phone" type="number" :border="false" placeholder="请输入手机号"
					maxlength="11"></up-input>
			</view>

			<!-- 验证码输入 -->
			<view class="input-box verification" v-if="showVerificationCode">
				<up-input v-model="phoneForm.verificationCode" type="number" :border="false" placeholder="请输入验证码"
					maxlength="6"></up-input>
			</view>


			<!-- 操作按钮 -->
			<view class="action-buttons">
				<up-button class="primary-btn" type="primary" :text="showVerificationCode ? '验证并登录' : '获取验证码'"
					@click="handlePhoneSubmit"></up-button>

				<view class="divider">
					<text>其他登录方式</text>
				</view>

				<view class="social-login">
					<view class="social-btn" @click="handleWeChatLogin">
						<image src="/static/img/weixin.png" mode="aspectFit"></image>
						<text>微信登录</text>
					</view>
				</view>
			</view>

			<!-- 用户协议 -->
			<view class="agreement">
				<view class="checkbox-wrap" @click="toggleAgreement">
					<view class="checkbox" :class="{ active: isAgreed }"></view>
				</view>
				<view class="agreement-text">
					<text>我已阅读并同意</text>
					<text class="link">《用户协议》</text>
					<text>和</text>
					<text class="link">《隐私政策》</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useLoginStore } from '@/stores/loginState.js'
import OpenAI from 'openai'
const store = useLoginStore()
const uToastRef = ref(null)
const showVerificationCode = ref(false)


const phoneForm = reactive({
	countryCode: '+86',
	phone: '',
	verificationCode: '',
	syncContacts: false
})

// 添加协议同意状态
const isAgreed = ref(false)

const selectCountry = () => {
	// TODO: 实现国家选择
	uni.showToast({
		title: '暂不支持其他地区',
		icon: 'none'
	})
}

const toggleAgreement = () => {
	isAgreed.value = !isAgreed.value
}

const handlePhoneSubmit = async () => {
	if (!isAgreed.value) {
		uToastRef.value.show({
			type: 'warning',
			message: '请先同意用户协议和隐私政策',
			position: 'top'
		})
		return
	}

	if (!phoneForm.phone) {
		uToastRef.value.show({
			type: 'warning',
			message: '请输入手机号',
			position: 'top'
		})
		return
	}

	if (phoneForm.phone.length !== 11) {
		uToastRef.value.show({
			type: 'warning',
			message: '请输入正确的手机号',
			position: 'top'
		})
		return
	}

	if (!showVerificationCode.value) {
		try {
			// TODO: 发送验证码的API调用
			showVerificationCode.value = true
			uToastRef.value.show({
				type: 'success',
				message: '验证码已发送',
				position: 'top'
			})
		} catch (error) {
			uToastRef.value.show({
				type: 'error',
				message: '验证码发送失败',
				position: 'top'
			})
		}
	} else {
		if (!phoneForm.verificationCode) {
			uToastRef.value.show({
				type: 'warning',
				message: '请输入验证码',
				position: 'top'
			})
			return
		}

		try {
			// TODO: 验证码验证API调用
			store.isLogin = true
			store.tokenInfo = {
				phone: phoneForm.phone,
				right: 'user'
			}
			uni.switchTab({
				url: '/pages/index/index'
			})
		} catch (error) {
			uToastRef.value.show({
				type: 'error',
				message: '验证码错误',
				position: 'top'
			})
		}
	}
}

const handleWeChatLogin = async () => {
	if (!isAgreed.value) {
		uToastRef.value.show({
			type: 'warning',
			message: '请先同意用户协议和隐私政策',
			position: 'top'
		})
		return
	}

	try {
		// #ifdef MP-WEIXIN
		const { code } = await uni.login({
			provider: 'weixin'
		})
		// TODO: 发送code到后端认证
		// #endif

		store.isLogin = true
		uni.switchTab({
			url: '/pages/chat/chat'
		})
	} catch (error) {
		uToastRef.value.show({
			type: 'error',
			message: '微信登录失败',
			position: 'top'
		})
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 90vh;
	background: linear-gradient(to bottom, #f8f8f8, #ffffff);
	padding: 40rpx;
	display: flex;
	flex-direction: column;
	justify-content: center;

	.header {
		margin-bottom: 80rpx;

		.title {
			font-size: 48rpx;
			font-weight: 600;
			color: #333;
			display: block;
			margin-bottom: 20rpx;
		}

		.subtitle {
			font-size: 28rpx;
			color: #999;
		}
	}

	.form {
		.input-box {
			background: #ffffff;
			border-radius: 16rpx;
			padding: 20rpx 30rpx;
			margin-bottom: 30rpx;
			display: flex;
			align-items: center;
			box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);

			.country-code {
				display: flex;
				align-items: center;
				padding-right: 20rpx;
				margin-right: 20rpx;
				border-right: 2rpx solid #eee;

				.arrow {
					margin-left: 10rpx;
					color: #999;
					transform: rotate(90deg);
				}
			}
		}

		.verification {
			margin-top: 30rpx;
		}

		.sync-option {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 0;
			color: #666;
		}

		.action-buttons {
			margin-top: 40rpx;

			.primary-btn {
				width: 100%;
				height: 90rpx;
				border-radius: 45rpx;
				font-size: 32rpx;
			}

			.divider {
				text-align: center;
				margin: 40rpx 0;
				color: #999;
				font-size: 28rpx;
				position: relative;

				&::before,
				&::after {
					content: '';
					position: absolute;
					top: 50%;
					width: 25%;
					height: 1rpx;
					background-color: #eee;
				}

				&::before {
					left: 50rpx;
				}

				&::after {
					right: 50rpx;
				}
			}

			.social-login {
				display: flex;
				justify-content: center;

				.social-btn {
					display: flex;
					flex-direction: column;
					align-items: center;
					padding: 20rpx;

					image {
						width: 80rpx;
						height: 80rpx;
						margin-bottom: 16rpx;
					}

					text {
						font-size: 24rpx;
						color: #666;
					}
				}
			}
		}

		.agreement {
			margin-top: 40rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 24rpx;
			color: #999;

			.checkbox-wrap {
				padding: 10rpx;
				margin-right: 10rpx;

				.checkbox {
					width: 32rpx;
					height: 32rpx;
					border: 2rpx solid #ddd;
					border-radius: 6rpx;
					position: relative;

					&.active {
						background-color: #2979ff;
						border-color: #2979ff;

						&::after {
							content: '';
							position: absolute;
							left: 50%;
							top: 50%;
							transform: translate(-50%, -50%) rotate(45deg);
							width: 6rpx;
							height: 12rpx;
							border: 2rpx solid #fff;
							border-width: 0 2rpx 2rpx 0;
							margin-top: -2rpx;
						}
					}
				}
			}

			.agreement-text {
				display: flex;
				align-items: center;
				flex-wrap: wrap;

				.link {
					color: #2979ff;
					padding: 0 4rpx;
				}
			}
		}
	}
}
</style>